package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.AppUserMark;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.market.config.MarketUserConfig;
import com.ruoyi.market.service.IMarketUserService;
import com.ruoyi.market.util.HttpConnetUtil;
import com.ruoyi.market.vo.MarketUserLabelVo;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销用户标签取数任务
 * @author pgc
 * @date: 2022-02-25
 */
@Slf4j
@Component("marketUserLabelTask")
public class MarketUserLabelTask {

    @Autowired
    IMarketUserService marketUserService;

    @Autowired
    MarketUserConfig marketUserConfig;

    /**
     * 电销取数标签配置
     */
    public static String MARKET_USER_LABEL_CONFIG ="market_user_label_config";

    /**
     * 电销取数标签配置状态
     */
    public static String status ="0";

    @Autowired
    ISysDictDataService sysDictDataService;

    /**
     * 根据标签及时间取数
     * @param userLabel beginDate endDate
     */
    public void pushUserLabelData(String userLabel , String beginDate ,String endDate){
        List<MarketUserLabelVo> marketUserLabelVos = new ArrayList<>();
        //如果接口参数中包含查询时间范围
        if(!userLabel.isEmpty() && !beginDate.isEmpty() && !endDate.isEmpty()){
            String[] userLabels = userLabel.split(",");
            for(int i=0; i<userLabels.length; i++){
                MarketUserLabelVo marketUserLabelVo = new MarketUserLabelVo();
                marketUserLabelVo.setUserLabel(userLabels[i]);
                marketUserLabelVo.setBeginDate(beginDate);
                marketUserLabelVo.setEndDate(endDate);
                marketUserLabelVos.add(marketUserLabelVo);
            }
        }else{//读取用户标签取数配置
            SysDictData params = new SysDictData();
            params.setDictType(MARKET_USER_LABEL_CONFIG);
            params.setStatus(status);
            List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(params);
            for(int i=0; i<sysDictDataList.size(); i++){
                SysDictData sysDictData = sysDictDataList.get(i);
                MarketUserLabelVo marketUserLabelVo = new MarketUserLabelVo();
                marketUserLabelVo.setUserLabel(sysDictData.getDictLabel());
                marketUserLabelVo.setIntervalHours(Integer.valueOf(sysDictData.getDictValue()));
                marketUserLabelVos.add(marketUserLabelVo);
            }
        }
        List<AppUserMark> appUserMarkList = marketUserService.selectAppUserMarkList(marketUserLabelVos);
        JSONObject jsonParam = marketUserFormatt(appUserMarkList,"add");
        //推送至营销系统
        if(jsonParam.getJSONArray("data").size()>0){
            String resultInfo = null;
            try {
                resultInfo = HttpConnetUtil.sendPost(marketUserConfig.getMarketUserImportUrl(), jsonParam.toJSONString());
            } catch (Exception e) {
                log.error("--------- 推送至营销系统 异常",e);
            }
            log.info("-------- 营销用户推送至 营销系统： " + resultInfo);
        }
    }

    /**
     * 更新用户标签
     * @param intervalTime
     */
    public void updateUserLabelData(String intervalTime){

        List<AppUserMark> appUserMarkList = marketUserService.selectAppUserMarkListByTime(Integer.valueOf(intervalTime));
        JSONObject jsonParam = marketUserFormatt(appUserMarkList,"update");
        //推送至营销系统
        if(jsonParam.getJSONArray("data").size()>0){
            String resultInfo = null;
            try {
                resultInfo = HttpConnetUtil.sendPost(marketUserConfig.getMarketUserImportUrl(), jsonParam.toJSONString());
            } catch (Exception e) {
                log.error("--------- 推送至营销系统 异常",e);
            }
            log.info("-------- 营销用户推送至 营销系统： " + resultInfo);
        }
    }

    /**
     * 推送营销系统用户数据拼接
     * @param appUserMarkList
     * @return
     */
    public JSONObject marketUserFormatt(List<AppUserMark> appUserMarkList, String type){
        JSONObject jsonParam = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for(AppUserMark marketUser : appUserMarkList){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userName",marketUser.getLastName());
            jsonObject.put("callPhone",marketUser.getRegisterPhone());
            jsonObject.put("bvnPhone",marketUser.getBvnPhone());
            jsonObject.put("userId",marketUser.getUserId());
            jsonObject.put("quota",marketUser.getCreditLine());
            jsonObject.put("custNo",marketUser.getCustNo());
            jsonObject.put("usedLimit",marketUser.getLoanQuota());
            jsonObject.put("firstTag",marketUser.getFirstTag());
            jsonObject.put("secondTag",marketUser.getSecondTag());
            jsonObject.put("gender",marketUser.getGender());
            jsonObject.put("birthday",marketUser.getBirthday());
            //本次提额额度
            jsonObject.put("increasedLimit",marketUser.getIncreasedAmt());
            jsonObject.put("increasedCreateTime",marketUser.getIncreasedCreateTime());
            jsonObject.put("remark",marketUser.getRemark());
            //增加版本号，用户等级
            jsonObject.put("appVersion",marketUser.getAppVersion());
            jsonObject.put("creditRating",marketUser.getCreditRating());
            jsonArray.add(jsonObject);
        }
        jsonParam.put("requestType",type);
        jsonParam.put("data",jsonArray);
        return jsonParam;
    }
}
